package com.dhc.appbusinessfrm.view.activity;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;

import com.dhc.appbusinessfrm.App;
import com.dhc.appbusinessfrm.R;
import com.dhc.appbusinessfrm.view.widget.ViewDialog;

import java.util.List;

import dagger.ObjectGraph;


public abstract class BaseActivity extends Activity implements BaseInteractor {
    private ObjectGraph activityGraph;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityGraph = ((App) getApplication()).createPlusGraph(getModules().toArray());
        activityGraph.inject(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        activityGraph = null;
    }

    @Override
    public void finishActivity() {
        finish();
    }

    protected abstract List<Object> getModules();

    /**
     * 创建网络不可用对话框
     */
    @Override
    public void createNetworkNotAvailableDialog() {
        // 创建Builder
        ViewDialog.Builder builder = new ViewDialog.Builder(this);
        // 设置标题
        builder.setTitle(R.string.prompt_label);
        // 设置信息
        builder.setMessage(R.string.to_network_setting_label);
        // 设置Positive按钮点击监听
        builder.setPositiveButton(R.string.btn_ok,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 进入无线网络配置界面
                        startActivity(new Intent(
                                Settings.ACTION_WIRELESS_SETTINGS));
                        // 本画面关闭
                        finish();
                        // 对话框关闭
                        dialog.dismiss();
                    }
                });

        // 设置Negative按钮点击监听
        builder.setNegativeButton(R.string.no_thanks_label,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 本画面关闭
                        finish();
                        // 对话框关闭
                        dialog.dismiss();
                    }
                });
        // 创建对话框并显示
        builder.create().show();
    }

    @Override
    public Context getMyContext() {
        return this;
    }

    @Override
    public ObjectGraph getActivityGraph() {
        return this.activityGraph;
    }

    /**
     * 显示短提示
     *
     * @param resId 资源ID
     */
    public void showShortPrompt(int resId) {
        showShortPrompt(getString(resId));
    }

    /**
     * 显示短提示
     *
     * @param promptContent 提示内容
     */
    public void showShortPrompt(String promptContent) {
        Toast.makeText(this, promptContent, Toast.LENGTH_SHORT).show();
    }

    /**
     * 显示长提示
     *
     * @param resId
     *            资源ID
     */
    public void showLongPrompt(int resId) {
        showLongPrompt(getString(resId));
    }

    /**
     * 显示长提示
     *
     * @param promptContent
     *            提示内容
     */
    public void showLongPrompt(String promptContent) {
        Toast.makeText(this, promptContent, Toast.LENGTH_LONG).show();
    }

}
